<%@ page contentType="text/html; charset=utf-8" %>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <link rel="icon" href="https://static.jianshukeji.com/highcharts/images/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://code.highcharts.com.cn/highcharts/highcharts.js"></script>
    <script src="https://code.highcharts.com.cn/highcharts/modules/exporting.js"></script>
    <script src="https://img.hcharts.cn/highcharts-plugins/highcharts-zh_CN.js"></script>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
    <script src="static/js/jquery-3.2.1.min.js"></script>
</head>
<body>
<!--
*************************************************************************
   Generated by JShare at 2019-06-12 21:10:22
   From: https://jshare.com.cn/temp/E5VkDu
*************************************************************************
 -->
<div id="container" style="min-width:400px;height:400px"></div>
<script>
    function testAsync() {
        //定义一个全局变量来接受$post的返回值</span><br> <span style="color: rgb(0, 0, 136)">var</span><span style="color: rgb(0, 0, 0)"> result</span><span style="color: rgb(102, 102, 0)">;</span><br> <span style="color: rgb(136, 0, 0)">//调用一般处理程序</span><br><span style="color: rgb(0, 0, 0)"> &nbsp; &nbsp;
        $.post("http://localhost:8080/BiZhangCar/home/info", {Sqls: sql4},
            function (courseDT4) {
                result = courseDT4;
            });
        return result;
    }


    /**
     * （1）同步调用 （2）且在ajax对全局变量进行设值 (3)ajax函数外将变量return
     * 结果：返回 2。成功获取返回值
     * 成功原因：先执行result = 2;再往下执行return result;
     */
    function checkAccount3() {
        var result = 1;
        $.ajax({
            url: 'http://localhost:8080/BiZhangCar/home/info',
            type: "post",
            data: {},
            async: false,
            success: function (data) {
                console.log("//////////"+data);
                result = data;
            }
        });
        return result;
    }



        Highcharts.setOptions({
        global: {
            useUTC: false
        }
    });

    function activeLastPointToolip(chart) {
        var points = chart.series[0].points;
        chart.tooltip.refresh(points[points.length - 1]);
    }

    var chart = Highcharts.chart('container', {
        chart: {
            type: 'spline',
            marginRight: 10,
            events: {
                load: function () {
                    var series = this.series[0],
                        chart = this;
                    activeLastPointToolip(chart);
                    setInterval(function () {
                        var x = (new Date()).getTime();// 当前时间
                         // y = Math.random()*100+10;          // 随机
                        // var y = 3;
                        // (function () {
                        //     y = 4;
                        // })()
                        var y=checkAccount3();
                        // console.log("))))))"+y);
                        // console.log(typeof y);
                        y=parseFloat(y);
                        series.addPoint([x, y], true, true);
                        activeLastPointToolip(chart);
                    }, 2000);
                }
            }
        },
        title: {
            text: '动态模拟实时数据'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 150
        },
        yAxis: {
            title: {
                text: null
            }
        },
        tooltip: {
            formatter: function () {
                return '<b>' + this.series.name + '</b><br/>' +
                    Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                    Highcharts.numberFormat(this.y, 2);
            }
        },
        legend: {
            enabled: false
        },
        series: [{
            name: '随机数据',
            data: (function () {
                // 生成随机值
                var data = [],
                    time = (new Date()).getTime(),
                    i;
                for (i = -5; i <= 0; i += 1) {
                    data.push({
                        x: time + i * 1000,
                        y: Math.random() * 20 + 10
                    });
                }
                return data;
            }())
        }]
    });</script>
</body>
</html>